package com.tanhua.recommend.listeners;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.model.db.Movement;
import com.tanhua.model.db.MovementScore;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @auther : 白月尘
 * @date : 17:21  2021/10/31
 */
@Component
public class RecommendMovementListener {
    @Autowired
    private MongoTemplate mongoTemplate;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(
                            name = "tanhua.movement.queue",
                            durable = "true"
                    ),
                    exchange = @Exchange(
                            name = "tanhua.log.exchange",
                            type = ExchangeTypes.TOPIC
                    ),
                    key = "log.movement"
            )
    )
    public void recommend(String message) {
        System.out.println("接受的消息为:"+message);
        Map<String, String> map = JSON.parseObject(message, Map.class);
        Long userId = Long.valueOf(map.get("userId"));
        String type = map.get("type");
        String movementId = map.get("busId"); //动态id
        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);
        if(movement != null) {
            //根据评分规则，计算评分
            MovementScore ms = new MovementScore();
            ms.setUserId(userId);
            ms.setMovementId(movement.getId());
            ms.setDate(System.currentTimeMillis());
            Double score = getScore(type,movement);
            ms.setScore(score);
            //3、保存到MongoDB
            mongoTemplate.save(ms);
        }
    }
    private static Double getScore(String type,Movement movement) {
        //0201为发动态  基础5分 50以内1分，50~100之间2分，100以上3分
        //0202为浏览动态， 1
        //0203为动态点赞， 5
        //0204为动态喜欢， 8
        //0205为评论，     10
        //0206为动态取消点赞， -5
        //0207为动态取消喜欢   -8
        Double score = 0d;
        switch (type) {
            case "0201":
                score = 5d;
                score += movement.getMedias().size();
                int length = StrUtil.length(movement.getTextContent());
                if (length >= 0 && length < 50) {
                    score += 1;
                } else if (length < 100) {
                    score += 2;
                } else {
                    score += 3;
                }
                break;
            case "0202":
                score = 1d;
                break;
            case "0203":
                score = 5d;
                break;
            case "0204":
                score = 8d;
                break;
            case "0205":
                score = 10d;
                break;
            case "0206":
                score = -5d;
                break;
            case "0207":
                score = -8d;
                break;
            default:
                break;
        }
        return score;
    }

}
